<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>网络分析服务-多旅行商分析(物流配送)</title>
    <script type="text/javascript" include="bootstrap-css" src="../js/include-web.js"></script>
    <script type="text/javascript" src="../../dist/include-openlayers.js"></script>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%">
<div id="map" style="width: 100%;height:100%"></div>
<script type="text/javascript">
    var map, options,
        baseUrl = (window.isLocal ? document.location.protocol + "//" + document.location.host : "http://support.supermap.com.cn:8090")+"/iserver/services/map-changchun/rest/maps/长春市区图",
        serviceUrl = (window.isLocal ? document.location.protocol + "//" + document.location.host : "http://support.supermap.com.cn:8090")+"/iserver/services/transportationanalyst-sample/rest/networkanalyst/RoadNet@Changchun";
    var extent = [48.4, -7668.25, 8958.85, -55.58];
    var projection = new ol.proj.Projection({
        extent: extent,
        units: 'm'
    });
    new ol.supermap.MapService(baseUrl).getMapInfo(function (serviceResult) {
        var mapJSONObj = serviceResult.result;
        map = new ol.Map({
            target: 'map',
            controls: ol.control.defaults({attributionOptions: {collapsed: false}})
                .extend([new ol.supermap.control.Logo()]),
            view: new ol.View({
                center: [5000, -3700],
                zoom: 2,
                projection: projection
            })
        });
        options = ol.source.TileSuperMapRest.optionsFromMapJSON(baseUrl, mapJSONObj);
        var layer = new ol.layer.Tile({
            source: new ol.source.TileSuperMapRest(options)
        });
        map.addLayer(layer);
        findMTSPPathsProcess();
    });

    function findMTSPPathsProcess() {
        //添加配送点
        var facilityPoint1 = new ol.geom.Point([6000, -5500]);
        var facilityPoint2 = new ol.geom.Point([5500, -2500]);
        var facilityPoint3 = new ol.geom.Point([2500, -3500]);
        var facilityFeature1 = new ol.Feature(facilityPoint1);
        var facilityFeature2 = new ol.Feature(facilityPoint2);
        var facilityFeature3 = new ol.Feature(facilityPoint3);
        var iconStyle_facility = new ol.style.Style({
            image: new ol.style.Icon(({
                src: '../img/marker-gold.png'
            }))
        });
        facilityFeature1.setStyle(iconStyle_facility);
        facilityFeature2.setStyle(iconStyle_facility);
        facilityFeature3.setStyle(iconStyle_facility);
        var facilitySource = new ol.source.Vector({
            features: [facilityFeature1, facilityFeature2, facilityFeature3]
        });
        var facilityLayer = new ol.layer.Vector({
            source: facilitySource
        });

        //添加配送目的地
        var destPoint1 = new ol.geom.Point([5000, -5000]);
        var destPoint2 = new ol.geom.Point([6500, -3200]);
        var destFeature1 = new ol.Feature(destPoint1);
        var destFeature2 = new ol.Feature(destPoint2);
        var iconStyle_dest = new ol.style.Style({
            image: new ol.style.Icon(({
                src: '../img/marker.png'
            }))
        });
        destFeature1.setStyle(iconStyle_dest);
        destFeature2.setStyle(iconStyle_dest);
        var destSource = new ol.source.Vector({
            features: [destFeature1, destFeature2]
        });
        var destLayer = new ol.layer.Vector({
            source: destSource
        });

        //创建多旅行商分析参数实例
        var resultSetting = new SuperMap.TransportationAnalystResultSetting({
            returnEdgeFeatures: true,
            returnEdgeGeometry: true,
            returnEdgeIDs: true,
            returnNodeFeatures: true,
            returnNodeGeometry: true,
            returnNodeIDs: true,
            returnPathGuides: true,
            returnRoutes: true
        });
        var analystParameter = new SuperMap.TransportationAnalystParameter({
            resultSetting: resultSetting,
            weightFieldName: "length"  //"length"或者"time"
        });
        var findMTSPPathsParameter = new SuperMap.FindMTSPPathsParameters({
            centers: [new ol.geom.Point([6000, -5500]), new ol.geom.Point([5500, -2500]), new ol.geom.Point([2500, -3500])],
            isAnalyzeById: false,
            nodes: [new ol.geom.Point([5000, -5000]), new ol.geom.Point([6500, -3200])],
            hasLeastTotalCost: true,
            parameter: analystParameter
        });
        //进行查找
        new ol.supermap.NetworkAnalystService(serviceUrl).findMTSPPaths(findMTSPPathsParameter, function (serviceResult) {
            serviceResult.result.pathList.map(function (result) {
                var vectorSource = new ol.source.Vector({
                    features: (new ol.format.GeoJSON()).readFeatures(result.route)
                });
                var pathLayer = new ol.layer.Vector({
                    source: vectorSource,
                    style: new ol.style.Style({
                        stroke: new ol.style.Stroke({
                            color: 'rgba(100, 100, 225, 10)',
                            width: 3
                        }),
                        fill: new ol.style.Fill({
                            color: 'rgba(0, 0, 255, 0.1)'
                        })
                    })
                });
                map.addLayer(pathLayer);
            });
            map.addLayer(facilityLayer);
            map.addLayer(destLayer);
        });
    }
</script>
</body>
</html>